<?php

App::uses('AppController', 'Controller');

/**
 * Categories Controller
 *
 * @property Category $Category
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class CategoriesController extends AppController
{

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator', 'Session');

    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->layout = 'admin';
        $this->Category->recursive = 0;
        $this->set('categories', $this->Paginator->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        $this->layout = 'admin';
        if (!$this->Category->exists($id))
        {
            throw new NotFoundException(__('Invalid category'));
        }
        $options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
        $this->set('category', $this->Category->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        $this->layout = 'admin';
        if ($this->request->is('post'))
        {
            $this->Category->create();
            if ($this->Category->save($this->request->data))
            {
                $this->Session->setFlash(__('The category has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
            }
        }
        $parentCategories = $this->Category->find('all', array('conditions'=>array('Category.parent_id'=>0),'fields' => array('id', 'name_category')));
        $this->set(compact('parentCategories'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        $this->layout = 'admin';
        if (!$this->Category->exists($id))
        {
            throw new NotFoundException(__('Invalid category'));
        }
        if ($this->request->is(array('post', 'put')))
        {
            if ($this->Category->save($this->request->data))
            {
                $this->Session->setFlash(__('The category has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
            }
        } else
        {
            $options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
            $this->request->data = $this->Category->find('first', $options);
        }
        $parentCategories = $this->Category->find('all', array('conditions'=>array('parent_id'=>0),'fields' => array('id', 'name_category')));
        $this->set(compact('parentCategories'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        $this->layout = 'admin';
        $this->Category->id = $id;
        if (!$this->Category->exists())
        {
            throw new NotFoundException(__('Invalid category'));
        }
        $this->request->allowMethod('post', 'delete');
        if ($this->Category->delete())
        {
            $this->Session->setFlash(__('The category has been deleted.'));
        } else
        {
            $this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }

}
